<?php

function list_gol_grade(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'Gol '=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Grade'=>array('style'=>'text-align:left;border-bottom:2px solid;width:80%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM golongan_jabatan ORDER BY golongan_jabatan_id ASC";
	$result = my_query($query);
	$row = array();

	while($ey = my_fetch_array($result) ){
		$query = "SELECT MIN(grade_id) as min, MAX(grade_id) as max FROM gol_grade where golongan_jabatan_id=".$ey['golongan_jabatan_id'];
		$re = my_query($query);
		$grades = my_fetch_array($re);

		$editproperty = array(
				'href'=>'index.php?com=gol_grade&task=edit&id='.$ey['golongan_jabatan_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
			);	
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$row[] = array(
			'gol'=>  $ey['kode_golongan_jabatan'] ,     
			'grade'=>  sprintf('%02d' ,$grades['min']).' - '.sprintf('%02d' ,$grades['max']) ,     
			'operasi'=> position_text_align( $edit_button , 'right'),
		);
	}
	$datas = table_rows($row);
	return table_builder($header , $datas ,  5 , false );;
}

function form_gol_grade_create($id){

	$view = form_header( "Grade" , "Grade"  );
	$gols=my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$id);
	$view .= form_field_display( $gols['kode_golongan_jabatan']   , "Golongan Jabatan"   );

	$query = "SELECT MIN(grade_id) as min, MAX(grade_id) as max FROM gol_grade where golongan_jabatan_id=".$id;
	$re = my_query($query);
	$fields = my_fetch_array($re);

	$query = "SELECT * FROM grade  ORDER BY grade_id ASC";
	// ORDER BY kode_grade ASC
	$result = my_query($query);
	$grades = array();
	while( $r = my_fetch_array($result) ){
		$grades[$r['grade_id']] =$r['kode_grade'];
	}

	$grade_min= array(
		'name'=>'grade_min',
		'value'=>( isset($_POST['grade_min'] ) ? $_POST['grade_min'] : $fields['min']  ),
		'id'=>'grade_min'  
	);
	$form_grade_min = form_dropdown($grade_min , $grades );

	$grade_max= array(
		'name'=>'grade_max',
		'value'=>( isset($_POST['grade_max'] ) ? $_POST['grade_max'] : $fields['max']  ),
		'id'=>'grade_max'  
	);
	$form_grade_max = form_dropdown($grade_max , $grades );
	
	$view .= form_field_display( $form_grade_min.' - '.$form_grade_max , "Grade " );

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );

	$view .= form_footer( );	
	return $view;
}

function form_gol_grade_validate($id){
	$errsubmit = false;
	$err = array();

	if($_POST["grade_min"] > $_POST["grade_max"]){
		$errsubmit = true;
		$err[] = "Pengisian grade belum benar";
	}

	if( $errsubmit){
		return $err;
	}

	return $errsubmit;
}


function form_gol_grade_submit($id = 0){
	$query = "DELETE FROM gol_grade WHERE golongan_jabatan_id ={$id} ";
	my_query($query);
	$count = $_POST['grade_min'];
	while ($count <= $_POST['grade_max'])
	{
		$datas = array(
				'golongan_jabatan_id'	=>my_type_data_str($id), 		 	 	 	 	 	 		
				'grade_id'=>my_type_data_int($count), 		 	 	 	 	 	 	 
				);
		my_insert_record('gol_grade' , $datas);	
		$count++;
	}
	return true ;
}
